package com.icygen.eclipsetools.ui.handlers;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeSelection;

import com.icygen.eclipsetools.ui.dialogs.ConfigApplicationDialog;


import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.dltk.core.DLTKCore;
import org.eclipse.dltk.core.IType;
import org.eclipse.dltk.core.ISourceModule;



/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class ConfigApplicationHandler extends AbstractHandler 
{

	/**
	 * The constructor.
	 */
	public ConfigApplicationHandler() 
	{
		
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException 
	{
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		
		if(window != null)
	    {
	        IStructuredSelection selection = (IStructuredSelection) window.getSelectionService().getSelection();
	        Object firstElement = selection.getFirstElement();
	        if (firstElement instanceof IAdaptable)
	        {
	            IProject project = (IProject)((IAdaptable)firstElement).getAdapter(IProject.class);
	            
	            
	            /*
	            try {
	            	project.open(null  IProgressMonitor );
		    		IScriptProject PHPProject = DLTKCore.create(project);
		    		IType lwType = PHPProject.findType("Users_Model_Entity_UsersInfo");
		    		ISourceModule lwSourceModule = lwType.getSourceModule();
		    		
		    		
		    		IType[] types = lwSourceModule.getAllTypes();
		    		
		    		for(IType type : types) {
		    			System.out.println( type.getFullyQualifiedName() + "\n");
		    		}

	            }
		    	catch(Exception e) {
		    		
		    	}
		    	*/
	 
	            ConfigApplicationDialog dialog = new ConfigApplicationDialog(window.getShell(), project);
	    		dialog.open();
	        }
	    }
		
		
		
		/*
		 * Get Selected Object
		 */
		/*
	    IWorkbenchPage activePage = window.getActivePage();
	    ISelection selection = activePage.getSelection();
	    if (selection != null) {
	        System.out.println("Got selection");
	        if (selection instanceof IStructuredSelection) {
	            System.out.println("Got a structured selection");
	            if (selection instanceof ITreeSelection) {
	                TreeSelection treeSelection = (TreeSelection) selection;
	                TreePath[] treePaths = treeSelection.getPaths();
	                TreePath treePath = treePaths[0];

	                System.out.println("Last");
	                Object lastSegmentObj = treePath.getLastSegment();
	                Class currClass = lastSegmentObj.getClass();
	                while(currClass != null) {
	                    System.out.println("  Class=" + currClass.getName());
	                    Class[] interfaces = currClass.getInterfaces();
	                    for(Class interfacey : interfaces) {
	                        System.out.println("   I=" + interfacey.getName());
	                    }
	                    currClass = currClass.getSuperclass();
	                }
	                if(lastSegmentObj instanceof IAdaptable) {
	                    IFile file = (IFile) ((IAdaptable) lastSegmentObj).getAdapter(IFile.class);
	                    if(file != null) {
	                        System.out.println("File=" + file.getName());
	                        String path = file.getRawLocation().toOSString();
	                        System.out.println("path: " + path);
	                    }
	                }
	            }
	        }
	    }
	    */
		
	
		return null;
	}
}
